package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.SysArticleResp;
import com.ruoyi.system.domain.vo.SysCollectResp;
import com.ruoyi.system.domain.vo.SysCommentResp;
import com.ruoyi.system.service.ISysArticleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysCollectMapper;
import com.ruoyi.system.domain.SysCollect;
import com.ruoyi.system.service.ISysCollectService;

/**
 * 收藏Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-02
 */
@Service
public class SysCollectServiceImpl implements ISysCollectService 
{
    @Autowired
    private SysCollectMapper sysCollectMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询收藏
     * 
     * @param userId 收藏主键
     * @return 收藏
     */
    @Override
    public SysCollect selectSysCollectByUserId(Long userId)
    {
        return sysCollectMapper.selectSysCollectByUserId(userId);
    }

    /**
     * 查询收藏列表
     * 
     * @param sysCollect 收藏
     * @return 收藏
     */
    @Override
    public List<SysCollectResp> selectSysCollectList(SysCollect sysCollect)
    {
        return sysCollectMapper.selectSysCollectList(sysCollect)
                .stream().map(this::buildCollect).collect(Collectors.toList());
    }

    @Override
    public List<SysCollectResp> selectSysCollectListByUserId(SysCollect sysCollect) {
        sysCollect.setUserId(SecurityUtils.getUserId());
        return sysCollectMapper.selectSysCollectList(sysCollect)
                .stream().map(this::buildCollect).collect(Collectors.toList());
    }

    private SysCollectResp buildCollect(SysCollect collect) {
        SysCollectResp resp = BeanUtil.copyProperties(collect, SysCollectResp.class);
        SysArticleResp article = SpringUtil.getBean(ISysArticleService.class).selectSysArticleById(collect.getArticleId());
        SysUser sysUser = userService.selectUserById(collect.getUserId());
        resp.setNickname(sysUser.getNickName());
        resp.setAvatar(sysUser.getAvatar());
        resp.setArticleName(article.getTitle());
        return resp;
    }

    /**
     * 新增收藏
     * 
     * @param sysCollect 收藏
     * @return 结果
     */
    @Override
    public int insertSysCollect(SysCollect sysCollect)
    {
        Long userId = SecurityUtils.getUserId();
        SysCollect find = sysCollectMapper.selectSysCollectByArticleIdAndUserId(sysCollect.getArticleId(), userId);
        if (Objects.nonNull(find)) {
            sysCollectMapper.deleteSysCollectArticleIdAndUserId(sysCollect.getArticleId(), userId);
            return 0;
        }
        sysCollect.setUserId(userId);
        return sysCollectMapper.insertSysCollect(sysCollect);
    }

    /**
     * 修改收藏
     * 
     * @param sysCollect 收藏
     * @return 结果
     */
    @Override
    public int updateSysCollect(SysCollect sysCollect)
    {
        return sysCollectMapper.updateSysCollect(sysCollect);
    }

    /**
     * 批量删除收藏
     * 
     * @param userIds 需要删除的收藏主键
     * @return 结果
     */
    @Override
    public int deleteSysCollectByUserIds(Long[] userIds)
    {
        return sysCollectMapper.deleteSysCollectByUserIds(userIds);
    }

    /**
     * 删除收藏信息
     * 
     * @param userId 收藏主键
     * @return 结果
     */
    @Override
    public int deleteSysCollectByUserId(Long userId)
    {
        return sysCollectMapper.deleteSysCollectByUserId(userId);
    }

    @Override
    public Integer selectSysCollectByArticleId(Long articleId) {
        return sysCollectMapper.selectSysCollectByArticleId(articleId);
    }

    @Override
    public int frontRemove(Long userId, Long articleId) {
        return sysCollectMapper.deleteSysCollectByUserIdAndArticleId(userId, articleId);
    }

    @Override
    public Integer selectCount() {
        return sysCollectMapper.selectCount();
    }

    @Override
    public SysCollect selectSysCollectByUserIdAndCollectId(Long userId, Long articleId) {
        return sysCollectMapper.selectSysCollectByArticleIdAndUserId(articleId, userId);
    }
}
